{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Java9 Samples\n", "\n", "From:\n", "\n", "http://hg.openjdk.java.net/kulla/dev/langtools/file/cf2fa1607bd0/repl/samples" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import java.util.*;\n", "int n = 12;\n", "int p = 0, c = 1, a; \n", "List list = new ArrayList<>();\n", "\n", "while (n-- > 0) {\n", " list.add(c);\n", " a = p + c;\n", " p = c;\n", " c = a;\n", "}\n", "\n", "System.out.println(list);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "int var1 = 2\n", "int meth2() { \n", " return var1+2; \n", "}\n", "\n", "++var1\n", " \n", "class class3 { \n", " int meth4() { \n", " return var1 * meth2(); \n", " } \n", "}\n", "\n", "System.out.println(new class3().meth4())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "int passCnt\n", "int failCnt\n", "int unexpectedCnt\n", "\n", "void check(boolean pass) {\n", " if (pass) {\n", " System.out.println(\"pass\");\n", " ++passCnt;\n", " } else {\n", " System.out.println(\"FAIL\");\n", " ++failCnt;\n", " }\n", "}\n", "\n", "void expectError(boolean huh) {\n", " System.out.println(\"Should not be here, should be error\");\n", " ++unexpectedCnt;\n", "}\n", "\n", "int m() { return 100; }\n", "int m(int x) { return x; }\n", "int m(int x, int y) { return x * y; }\n", "\n", "check(m(5,4) == 20);\n", "check(m(7) == 7);\n", "check(m() == 100);\n", "int m(int x) { return x * 100; }\n", "check(m(3) == 300);\n", "check(m() == 100);\n", "check(m(5,4) == 20);\n", "int m() { return 1234; }\n", "check(m() == 1234);\n", "double m(int x, int y) { return x + y; }\n", "check(m(2,3) == 5.0);\n", "double m(double x, double y) { return y - x; }\n", "check(m(2.0,3.0) == 1.0);\n", "check(m(2,3) == 5.0);\n", "System.out.println((passCnt==10 && failCnt==0 && unexpectedCnt==0)? \n", " \"Test passes!\" : \n", " \"Test FAILED! Pass: \" + passCnt + \", Fail: \" + failCnt + \", Unexpected: \" + unexpectedCnt)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " System.out.println(\"Ok\")\n", "Ok\n", "\n", "\n" ] } ], "source": [ "int passCnt;\n", "int failCnt;\n", "int unexpectedCnt;\n", "/*\n", "void check(boolean pass) {\n", " if (pass) {\n", " System.out.println(\"pass\");\n", " ++passCnt;\n", " } else {\n", " System.out.println(\"FAIL\");\n", " ++failCnt;\n", " }\n", "}\n", "*/\n", "/*\n", "void expectError(boolean huh) {\n", " System.out.println(\"Should not be here, should be error\");\n", " ++unexpectedCnt;\n", "}\n", "*/\n", "int x;\n", "//int mu() { return x * 4; }\n", "//class C {\n", "// String v() { return \"#\" + mu(); }\n", "//}\n", "//new C();\n", "//check($1.v().equals(\"#0\"))\n", " /*\n", "int x = 10\n", "check($1.v().equals(\"#40\"))\n", "C c = new C()\n", "check(c.v().equals(\"#40\"))\n", "int mu() { return x * 3; }\n", "check(c.v().equals(\"#30\"))\n", "class C {\n", " String v() { return \"@@\" + mu(); }\n", "}\n", "check($1.v().equals(\"@@30\"))\n", "c = new C()\n", "check(c.v().equals(\"@@30\"))\n", "double x = 2.5\n", "check(x == 2.5)\n", "*/\n", "/*\n", "expectError(mu() == 30)\n", "c = new C()\n", "expectError(c.v().equals(\"@@30\"))\n", "int mu() { return (int) (x * 10); }\n", "check(mu() == 25)\n", "c = new C()\n", "check(c.v().equals(\"@@25\"))\n", "double mu() { return x * 2.0; }\n", "check(mu() == 5.0)\n", "check($1.v().equals(\"@@5.0\"))\n", "c = new C()\n", "check(c.v().equals(\"@@5.0\"))\n", "class C {\n", " Object v() { return mu(); }\n", "}\n", "//check($1 == null) //TODO fix update of temp vars\n", "C d = new C()\n", "check(d.v().equals(5.0))\n", "\n", "System.out.println((passCnt==13 && failCnt==0 && unexpectedCnt==0)? \n", " \"Test passes!\" : \n", " \"Test FAILED! Pass: \" + passCnt + \", Fail: \" + failCnt + \", Unexpected: \" + unexpectedCnt)\n", " */\n", "System.out.println(\"Ok\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " System.out.println(\"Ok\")\n", "Ok\n", "\n", "\n" ] } ], "source": [ "System.out.println(\"Ok\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " tree.left = new Tree(2);\n", "| Expression value is: Tree@33e5ccce\n", "| assigned to temporary variable $1 of type Tree\n", "\n", "\n" ] }, { "data": { "text/plain": [ "Tree@33e5ccce" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Tree {\n", " Tree left;\n", " Tree right;\n", " int value;\n", " \n", " Tree(int value) {\n", " this.value = value;\n", " }\n", "}\n", "\n", "Tree tree = new Tree(10);\n", "tree.left = new Tree(2);" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " tree.left.value\n", "| Expression value is: 2\n", "| assigned to temporary variable $2 of type int\n", "\n", "\n" ] }, { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tree.left.value" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " $2\n", "| Variable $2 of type int has value 2\n", "\n", "\n" ] }, { "data": { "text/plain": [ "2" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "$2" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " System.out.println(y)\r\n", "| Error:\r\n", "| cannot find symbol\r\n", "| symbol: variable y\r\n", "| System.out.println(y)\r\n", "| ^\r\n", "\r\n", "\n" ] } ], "source": [ "System.out.println(y)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " System.out.println(1/0)\r\n", "| java.lang.ArithmeticException thrown: / by zero\r\n", "| at (#9:1)\r\n", "\r\n", "\n" ] } ], "source": [ "System.out.println(1/0)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " System.out.println(\"ok\")\n", "ok\n", "\n", "\n" ] } ], "source": [ "System.out.println(\"ok\")" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%python\n", "\n", "kernel.javawrapper.child.send(\"varia\")\n", "kernel.javawrapper.child.sendcontrol('t')\n", "kernel.javawrapper.child.sendeof()\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " varia\u0007\u0007System.out.println(\"ok\")\r\n", "| Error:\r\n", "| package variaSystem does not exist\r\n", "| variaSystem.out.println(\"ok\")\r\n", "| ^-------------^\r\n", "\r\n", "\n" ] } ], "source": [ "System.out.println(\"ok\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " /help\n", "Type a Java language expression, statement, or declaration.\n", "Or type one of the following commands:\n", "\n", "/l or /list [all] -- list the source you have typed\n", " /seteditor -- set the external editor command to use\n", "/e or /edit -- edit a source entry referenced by name or id\n", "/s or /save [all|history] -- save the source you have typed\n", "/o or /open -- open a file as source input\n", "/v or /vars -- list the declared variables and their values\n", "/m or /methods -- list the declared methods and their signatures\n", "/c or /classes -- list the declared classes\n", "/x or /exit -- exit the REPL\n", "/r or /reset -- reset everything in the REPL\n", "/f or /feedback -- feedback information: off, concise, normal, verbose, default, or ?\n", "/p or /prompt -- toggle display of a prompt\n", "/cp or /classpath -- add a path to the classpath\n", "/h or /history -- history of what you have typed\n", " /setstart -- read file and set as the new start-up definitions\n", " /savestart -- save the default start-up definitions to the file\n", "/? or /help -- this help message\n", "\n", "Supported shortcuts include:\n", " -- show possible completions for the current text\n", "Shift- -- for current method or constructor invocation, show a synopsis of the method/constructor\n", "\n", "\n" ] } ], "source": [ "/help" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " /classes\n", "\n", "\n" ] } ], "source": [ "/help" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " /help\n", "Type a Java language expression, statement, or declaration.\n", "Or type one of the following commands:\n", "\n", "/l or /list [all] -- list the source you have typed\n", " /seteditor -- set the external editor command to use\n", "/e or /edit -- edit a source entry referenced by name or id\n", "/s or /save [all|history] -- save the source you have typed\n", "/o or /open -- open a file as source input\n", "/v or /vars -- list the declared variables and their values\n", "/m or /methods -- list the declared methods and their signatures\n", "/c or /classes -- list the declared classes\n", "/x or /exit -- exit the REPL\n", "/r or /reset -- reset everything in the REPL\n", "/f or /feedback -- feedback information: off, concise, normal, verbose, default, or ?\n", "/p or /prompt -- toggle display of a prompt\n", "/cp or /classpath -- add a path to the classpath\n", "/h or /history -- history of what you have typed\n", " /setstart -- read file and set as the new start-up definitions\n", " /savestart -- save the default start-up definitions to the file\n", "/? or /help -- this help message\n", "\n", "Supported shortcuts include:\n", " -- show possible completions for the current text\n", "Shift- -- for current method or constructor invocation, show a synopsis of the method/constructor\n", "\n", "\n" ] } ], "source": [ "/help" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%python\n", "\n", "kernel.javawrapper.child.read(10)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " int variable_name = 1;\n", "| Added variable variable_name of type int with initial value 1\n", "\n", "\n" ] } ], "source": [ "int variable_name = 1;" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " variable_name\n", "| Variable variable_name of type int has value 1\n", "\n", "\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variable_name" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " variable_name\n", "| Variable variable_name of type int has value 1\n", "\n", "\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variable_name" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " variable_name\n", "| Variable variable_name of type int has value 1\n", "\n", "\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variable_name" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " variable_name\n", "| Variable variable_name of type int has value 1\n", "\n", "\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variable_name" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Java 9", "language": "java", "name": "java" }, "language_info": { "file_extension": ".class", "mimetype": "application/java-vm", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }